/**
 * Created by spatra on 14-9-7.
 */

define(function(){

	/**
	 * 实体的构造函数
	 *
	 * @param radius 半径，默认值为1
	 * @param x 开始的坐标x，默认值0
	 * @param y 开始的坐标y， 默认值为0
	 * @param vx 速度vx，默认值为0
	 * @param vy 速度vy， 默认值为0
	 * @param ax 加速度ax， 默认值为0
	 * @param ay 加速度ay，默认值为0
	 *
	 * @constructor 实体的构造函数（类Entity）
	 */
	var Entity = function(radius, x, y, vx, vy, ax, ay){
		this.radius = radius || 1;

		this.x = x || 0;
		this.y = y || 0;

		this.vx = vx || 0;
		this.vy = vy || 0;

		this.ax = ax || 0;
		this.ay = ay || 0;


		this.collision = false;
		this.dragStatus = false;
	};

	/**
	 * 检查两个实体是否发生了碰撞.
	 *
	 * @param entityA
	 * @param entityB
	 * @returns {boolean}
	 */
	Entity.checkCollision = function(entityA, entityB){
		var diameter = entityA.radius + entityB.radius;

		var dx = entityB.x - entityA.x;
		var dy = entityB.y - entityA.y;

		var distance2 = dx * dx + dy * dy;

		if( distance2 <= diameter * diameter ){
			return true;
		}
		else{
			return false;
		}

	};

	Entity.prototype = {
		constructor: Entity,

		/**
		 * 设置实体的随机速度， 传入的为大小的绝对值，随机给予方向
		 *
		 * @param maxVel 可传入单一的数值，该数值作为vx和vy的最大值，也可以传入
		 *               对象{maxVx, maxVy}
		 */

		setRandomVelocity: function(maxVel){
			var maxVx, maxVy;
			var self = this;

			if( typeof maxVel === 'object' ){
				maxVx = maxVel.maxVx;
				maxVy = maxVel.maxVy;
			}
			else{
				maxVx = maxVy = maxVel;
			}

			function setVelocity(vel, maxVel){
				self[vel] = Math.random() < 0.5 ?
					( Math.random() * maxVel + 1 ) >> 0
					:
					( Math.random() * maxVel - 1 ) >> 0;

				if( Math.random() < 0.5 ){
					self[vel] *= -1;
				}
			}

			setVelocity('vx', maxVx);
			setVelocity('vy', maxVy);
		},

		setRandomRadius: function(maxRadius){
			this.radius = (Math.random() * maxRadius) >> 0;
		},

		setRandomPosition: function(containerWidth, containerHeight){
			this.x = (Math.random() * containerWidth) >> 0;
			this.y = (Math.random() * containerHeight) >> 0;
		},

		setRandomStatus: function(maxVel, maxRadius, containerWidth, containerHeight){
			this.setRandomVelocity(maxVel);
			this.setRandomRadius(maxRadius);
			this.setRandomPosition(containerWidth, containerHeight);
		},

		isSkipMove: function(){
			return this.dragStatus;
		}
	};

	return Entity;
});